package com.baosight.sczx.zm.ce.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.soa.SoaManager;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.zm.ce.domain.ZMCEEquipmentDynamic;

/**
 * <p>Title: ServiceZMCE31.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author  高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-14 下午02:40:20  高强  Create
 *</pre>
 */
public class ServiceZMCE31 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = query(inInfo);
		outInfo.addBlock(getMoveType());//变动类型
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", true));
		outInfo.addBlock(Engineering.getEngineering("cmes.ce.engineering", null, true));//工程
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		return outInfo;
	}

	/**
	 * 设备变动查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdPreparerDepId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdPreparerDepId", fdPreparerDepId, true);//添加查询条件 操作用户
		EiInfo outInfo = super.query(inInfo, "ZMCEEquipmentDynamic.query", new ZMCEEquipmentDynamic());				
		return outInfo;
	}
	
	/**
	 * 设备变动新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCEEquipmentDynamic.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_EQUIPMENTDYNAMIC"));//主键
			inInfo.getBlock("result").setCell(0, "fdPreparerId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(0, "fdPreparer", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdPreparerDepId", UserSession.getInSessionProperty("orgCode"));
			inInfo.getBlock("result").setCell(0, "fdStatus", "草拟");
			EiInfo outinfo = super.insert(inInfo, "ZMCEEquipmentDynamic.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCEEquipmentDynamic.update");
		return outInfo;
		
	}
	

	/**
	 * 设备变动删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEEquipmentDynamic.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}
	
	/**
	 * 更新发布状态为"生效"的方法
	 * <p>Function: publish</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo publish(EiInfo inInfo) 
	{		
		//更新设备卡片的使用状态和使用部门
		String type = "", intact = null;
		if(inInfo.getBlock("result").getCell(0, "fdType").toString().equals("领用")
				||inInfo.getBlock("result").getCell(0, "fdType").toString().equals("启封")
				||inInfo.getBlock("result").getCell(0, "fdType").toString().equals("移交"))
		{
			type = "在用";			
		}else if(inInfo.getBlock("result").getCell(0, "fdType").toString().equals("封存"))
		{
			type = "封存";
		}else if(inInfo.getBlock("result").getCell(0, "fdType").toString().equals("闲置"))
		{
			type = "闲置";
		}else if(inInfo.getBlock("result").getCell(0, "fdType").toString().equals("报废"))
		{
			type = "报废";
			intact = "不完好";
		}
		if(!"".equals(type))
			if(Equipment.updateEquipmentType(type,inInfo.getBlock("result").getCellStr(0, "fdNewDepartmentId"),
					inInfo.getBlock("result").getCellStr(0, "fdNewDepartment"),
					inInfo.getBlock("result").getCellStr(0, "fdNewEngineeringId"),
					inInfo.getBlock("result").getCellStr(0, "fdNewEngineering"),
					intact, inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
			{
				inInfo.setStatus(-1);
				return inInfo;
			}
		
		inInfo.getBlock("result").setCell(0, "fdStatus", "生效");
		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentDynamic.updateStatus");
		EiInfo outInfo = this.query(_temp);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}


	
	/**
	 * 获取变动类型的下拉框
	 * <p>Function: getMoveType</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private EiBlock getMoveType()
	{
		EiBlock block = new EiBlock("cmes.ce.type");
		
		EiColumn eiColumn = new EiColumn("label");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = new ArrayList<Map>();
		
		Map<String, String> _map;
		
		_map = new HashMap<String, String>();
		_map.put("value", "领用");
		_map.put("label", "领用");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "启封");
		_map.put("label", "启封");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "封存");
		_map.put("label", "封存");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "移交");
		_map.put("label", "移交");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "闲置");
		_map.put("label", "闲置");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "报废");
		_map.put("label", "报废");
		list.add(_map);	
		
		block.setRows(list);
		return block;
	}
	
//	/**
//	 * 获得工程
//	 * <p>Function: getEngineering</p>
//	 * <p>Description: </p>
//	 * <p>return value:EiBlock</p>
//	 * <p>throws Exception:</p>
//	 * <p>History:
//	 *    2011-12-16下午04:26:16 高强
//	 * </p>
//	 *
//	 */
//	@SuppressWarnings("rawtypes")
//	private EiBlock getEngineering()
//	{
//		EiBlock block = new EiBlock("cmes.ce.engineering");
//		
//		EiColumn eiColumn = new EiColumn("fdKeyId");
//		eiColumn.setType("C");
//		block.addMeta(eiColumn);
//		
//		eiColumn = new EiColumn("fdName");
//		eiColumn.setType("C");
//		block.addMeta(eiColumn);
//		
//		List<Map> list = Engineering.getEngineerings(null);
//		
//		block.setRows(list);
//		return block;
//	}
//	
	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:26:16 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdSerial = inInfo.getBlock("result").getCellStr(0, "fdCode");
		params.put("fdCode", fdSerial);
		List result = dao.query("ZMCEEquipmentDynamic.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			
			inInfo.setMsg("编号不能重复！");
			return true;			
		}
		return false;
	}
}
